<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--通用查询-->
      <queryHeader :dict="dict" :permission="permission" />
      <!--通用编辑-->
      <crudOperation :permission="permission" />
    </div>

    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <!--多选框-->
      <el-table-column type="selection" width="55" />
      <!--展示字段-->
      <el-table-column v-if="columns.visible('jobId')" prop="jobId" label="任务ID" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('jobName')" prop="jobName" label="任务名称" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('jobGroup')" prop="jobGroup" label="任务组名" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('methodName')" prop="methodName" label="任务方法" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('methodParams')" prop="methodParams" label="方法参数" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('cronExpression')" prop="cronExpression" label="cron执行表达式" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('misfirePolicy')" prop="misfirePolicy" label="计划执行错误策略" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.type_job_misfire[scope.row.misfirePolicy] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('concurrent')" prop="concurrent" label="是否并发执行" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.type_job_concurrent[scope.row.concurrent] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('status')" prop="status" label="状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.status_job[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('optTime')" prop="optTime" label="操作时间" :show-overflow-tooltip="true" />

      <!--操作-->
      <el-table-column label="操作" width="280px" align="center" fixed="right">
        <template slot-scope="scope">
          <!--通用编辑删除-->
          <udOperation :data="scope.row" :permission="permission">
            <template slot="left">
              <el-button size="mini" type="text" :loading="optLoading.run" @click="handRun(scope.row)">立即执行</el-button>
              <el-button size="mini" type="text" :loading="optLoading.resume" @click="handResume(scope.row)">重启</el-button>
              <el-button size="mini" type="text" :loading="optLoading.stop" @click="handStop(scope.row)">暂停</el-button>
            </template>
          </udOperation>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <pagination />

    <!--表单渲染-->
    <editForm :dict="dict" />
  </div>
</template>

<script>
import crudJobOpt, { pauseJob, resumeJob, runJob } from '@/api/system/job/opt'
import queryHeader from './module/queryHeader'
import editForm from './module/editForm'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'

// crud交由presenter持有
const crud = CRUD({ title: 'JobOpt', url: 'job/list/page', crudMethod: { ...crudJobOpt }, pkIdName: 'jobId' })

export default {
  name: 'JobOpt',
  components: { queryHeader, editForm, crudOperation, pagination, udOperation },
  dicts: ['type_job_misfire', 'type_job_concurrent', 'status_job'],
  mixins: [presenter(crud)],
  data() {
    return {
      // 对应几个按钮操作
      permission: {
        add: [],
        edit: [],
        del: []
      },
      optLoading: {
        run: false,
        resume: false,
        stop: false
      }
    }
  },
  methods: {
    handRun(row) {
      // this.optLoading.run = true
      runJob(row.jobId).then(() => {
        this.optLoading.run = false
        this.crud.notify('执行成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(() => {
        this.optLoading.run = false
      })
    },
    handResume(row) {
      // this.optLoading.resume = true
      resumeJob(row.jobId).then(() => {
        this.optLoading.resume = false
        this.crud.notify('重启成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(() => {
        this.optLoading.resume = false
      })
    },
    handStop(row) {
      // this.optLoading.stop = true
      pauseJob(row.jobId).then(() => {
        this.optLoading.stop = false
        this.crud.notify('暂停成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(() => {
        this.optLoading.stop = false
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
